﻿/*格式化日期*/
Date.prototype.format = function (format) {
    var o =
    {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(),    //day
        "h+": this.getHours(),   //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
        "s": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format))
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(format))
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}

/* 判断字符串是否以指定的字符串开始*/
String.prototype.startsWith = function (str) {
    return this.substr(0, str.length) == str;
}
/* 判断字符串是否以指定的字符串结束*/
String.prototype.endsWith = function (str) {
    return this.substr(this.length - str.length) == str;
}
/* 去掉字符串左端的的空白字符*/

String.prototype.leftTrim = function () {
    return this.replace(/(^[\\s]*)/g, "");
}
/* 去掉字符串右端的空白字符*/

String.prototype.rightTrim = function () {
    return this.replace(/([\\s]*$)/g, "");
}
/* 去掉字符串两端的空白字符*/

String.prototype.trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

/*去掉字符串两端指定的字符*/
String.prototype.trims = function (str) {
    var tmp = this;
    if (tmp.startsWith(str)) tmp = tmp.substr(str.length);
    if (tmp.endsWith(str)) tmp = tmp.substr(0, tmp.length - str.length);
    return tmp;
}

//数组中是否存在某个指定值
Array.prototype.Exist = function (_fieldName) {
    for (var i = 0; i < this.length; i++) {
        if (_fieldName == this[i])
            return true;
    }
    return false;
}

/*对a进行四舍五入并保留b位小数*/
function RoundNumber(a, b) {
    return (Math.round(a * Math.pow(10, b)) * Math.pow(10, -b));
}

/* 调用示例msg("错误提示",error,true,"",300,100,"20px;","#333333","#FFFFFF",60)*/
function msg(title, info, allowclose, clsfun, w, top, padding, bordercolor, bgcolor, alpha) {
    if (w == undefined) w = 200;
    if (top == undefined) top = 100;
    if (padding == undefined) padding = "20px;";
    if (bordercolor == undefined) bordercolor = "#333333";
    if (bgcolor == undefined) bgcolor = "#FFFFFF";
    if (alpha == undefined) alpha = 60;

    var p = document.createElement("DIV");
    p.id = "p";
    p.style.position = "absolute";
    p.style.width = document.body.scrollWidth;
    p.style.height = (document.body.offsetHeight > document.body.scrollHeight) ? '100%' : document.body.scrollHeight;
    p.style.zIndex = '998';
    p.style.top = '0px';
    p.style.left = '0%';
    p.style.backgroundColor = "gray";
    p.style.opacity = '0.5';
    p.style.filter = "alpha(opacity=" + alpha + ")";
    document.body.appendChild(p);
    var p1 = document.createElement("DIV");
    p1.style.position = "absolute";
    p1.style.width = w + "px";
    p1.id = "p1";
    var left = Math.ceil(((document.body.scrollWidth) - parseInt(p1.style.width.replace('px', '')))) / 2
    p1.style.height = "";
    p1.style.zIndex = '999';
    p1.style.top = top + document.body.scrollTop + 'px';
    p1.style.left = left + 'px';
    p1.style.border = "0px solid " + bordercolor + "";
    var html = "";
    html += "<center>"
    html += "<div class='p3' style='height:1px;overflow:hidden;background:" + bordercolor + ";width:" + (Number(w) - 6) + "px;border-left:1px solid " + bordercolor + ";border-right:1px solid " + bordercolor + ";'></div>"
    html += "<div class='p2' style='height:1px;overflow:hidden;background:" + bordercolor + ";width:" + (Number(w) - 4) + "px;border-left:1px solid " + bordercolor + ";border-right:1px solid " + bordercolor + ";'></div>"
    html += "<div class='p2' style='height:1px;overflow:hidden;background:" + bordercolor + ";width:" + (Number(w) - 2) + "px;border-left:1px solid " + bordercolor + ";border-right:1px solid " + bordercolor + ";'></div>"
    html += "<div class='p1' style='height:30px;overflow:hidden;background:" + bordercolor + ";width:" + w + "px;border-left:1px solid " + bordercolor + ";border-right:1px solid " + bordercolor + ";color:#fff;font-size:9pt;font-weight:bold;text-align:left; padding-top:5px;'>&nbsp;&nbsp;" + title + "</div>"
    if (allowclose) {
        var btm = "<br><p align=center>[ <a href='javascript:closeMsg();'>Close</a> ]</p>";
    } else {
        var btm = "";
    }
    //html += "<div id='c' style='width:" + w + "px;background-color:" + bgcolor + ";border-left:1px solid " + bordercolor + ";border-right:1px solid " + bordercolor + ";font-size:9pt;' align='left'><div style='padding:" + padding + ";'><IMG  SRC='" + getRootPath() + "/Images/indicator.gif'/>" + info + btm + "</div></div>"
    html += "<div id='c' style='width:" + w + "px;background-color:" + bgcolor + ";border-left:1px solid " + bordercolor + ";border-right:1px solid " + bordercolor + ";font-size:9pt;' align='left'><div style='padding:" + padding + ";'>" + info + btm + "</div></div>"
    html += "<div class='p1' style='height:1px;overflow:hidden;background:" + bgcolor + ";width:" + (Number(w) - 2) + "px;border-left:1px solid " + bordercolor + ";border-right:1px solid " + bordercolor + ";'></div>"
    html += "<div class='p2' style='height:1px;overflow:hidden;background:" + bgcolor + ";width:" + (Number(w) - 4) + "px;border-left:1px solid " + bordercolor + ";border-right:1px solid " + bordercolor + ";'></div>"
    html += "<div class='p3' style='height:1px;overflow:hidden;background:" + bordercolor + ";width:" + (Number(w) - 6) + "px;border-left:1px solid " + bordercolor + ";border-right:1px solid " + bordercolor + "'></div>"
    html += "</center>"
    document.body.appendChild(p1);
    p1.innerHTML = html;
    try {
        if ($$("select")) {
            var arr = $$("select");
            var i = 0;
            while (i < arr.length) {
                arr[i].style.visibility = 'hidden';
                i++;
            }
        }
        if ($$("iframe")[0]) {
            if ($$("iframe")[0].name) {
                iframe = $$("iframe")[0].name
                var arr2 = eval("window." + iframe + ".document.getElementsByTagName('select');");
                var i = 0
                while (i < arr2.length) {
                    arr2[i].style.visibility = 'hidden';
                    i++;
                }
            }
        }
        if ($$("iframe")[1]) {
            if ($$("iframe")[1].name) {
                iframe = $$("iframe")[1].name
                var arr3 = eval("window." + iframe + ".document.getElementsByTagName('select');");
                var i = 0
                while (i < arr3.length) {
                    arr3[i].style.visibility = 'hidden';
                    i++;
                }
            }
        }
    }
    catch (e) { }
}

function closeMsg() {
    $("#p").remove();
    $("#p1").remove();
}

//获取根目录
function getRootPath() {
    var strFullPath = window.document.location.href;
    var strPath = window.document.location.pathname;
    var pos = strFullPath.indexOf(strPath);
    var prePath = strFullPath.substring(0, pos);
    var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
    return (prePath + postPath);
    //return prePath;
}
/*=============================*/

function isNumber(obj) {
    var key = event.keyCode;
    /*alert(key);*/
    event.returnValue = false;
    if (key == 8 || key == 46 || (key >= 35 && key <= 39) || (key >= 96 && key <= 105) || key >= 48 && key <= 57)
        event.returnValue = true;

    if ((event.keyCode == 190 || event.keyCode == 110) && obj.value.indexOf(".") == -1)
        event.returnValue = true;
}

function IsNumberOrCharacters(obj) {
    var re = /^[A-Za-z0-9]*$/;
    var str = '';
    event.returnValue = false;
    if (re.test(obj.value))
        event.returnValue = true;
}

/*检查是否是有效的时间格式*/
/*用-分隔日期;日期、日期+时间有效*/
function IsDate(sDate) {
    //    var today=new Date(Date.parse(sDate.replace(/-/g,"/")));
    //    var year=today.getYear();

    //    var month=today.getMonth()+1;
    //    if(month<10)
    //     month="0"+month;

    //    var day=today.getDate();
    //     if(day<10)
    //      dat="0"+day;

    //    var hours = today.getHours();  
    //    var minutes = today.getMinutes();  
    //    var seconds = today.getSeconds(); 
    //     hours = ((hours < 10) ? "0" + hours : hours);  
    //     minutes = ((minutes < 10) ? "0" + minutes : minutes);   
    //     seconds = ((seconds < 10) ? "0" + seconds : seconds);

    //     var time_holder =year+"-"+month+"-"+day+" "+ hours + ":" + minutes + ":" + seconds;   

    //    var re = /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29))(\s(0?[1-9]|1[0-9]|2[0-3]):(0?[0-9]|[1|2|3|4|5][0-9]):(0?[0-9]|[1|2|3|4|5][0-9])){0,1}$/;
    //jerzy 2012-3-26 上面的通过"2012-03-26 00:00:00"的验证
    //     var re=/^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29))(\s(0?[0-9]|1[0-9]|2[0-3]):(0?[0-9]|[0|1|2|3|4|5][0-9]):(0?[0-9]|[0|1|2|3|4|5][0-9])){0,1}$/;
    // jerzy 2012-4-9 可以不输入秒
    var re = /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29))(\s(0?[0-9]|1[0-9]|2[0-3]):(0?[0-9]|[0|1|2|3|4|5][0-9])(:(0?[0-9]|[0|1|2|3|4|5][0-9]))?){0,1}$/;
    //if (re.test(time_holder))
    if (re.test(sDate.trim()))
        return true;
    else
        return false;
}

/*jQuery select==============*/
jQuery.fn.size = function () {
    try {
        return jQuery(this).get(0).options.length;
    } catch (e) { }
}
/*获得选中项的索引 */
jQuery.fn.getSelectedIndex = function () {
    return jQuery(this).get(0).selectedIndex;
}
/*获得当前选中项的文本 */
jQuery.fn.getSelectedText = function () {
    if (this.size() == 0) {
        //return "下拉框中无选项"; 
        return "";
    }
    else {
        var index = this.getSelectedIndex();
        return jQuery(this).get(0).options[index].text;
    }
}
/*获得当前的文本 */
jQuery.fn.getSelectedAllText = function () {
    if (this.size() == 0) {
        //return "下拉框中无选项"; 
        return "";
    }
    else {
        var res = "";
        for (var i = 0; i < jQuery(this).get(0).length; i++) {
            res += jQuery(this).get(0).options[i].text + ",";
        }
        return res.trims(",");
    }
}
/*获取当前的值*/
jQuery.fn.getSelectedAllValue = function () {
    if (this.size() == 0) {
        //return "下拉框中无选项";
        return "";
    }
    else {
        var res = "";
        for (var i = 0; i < jQuery(this).get(0).length; i++) {
            res += jQuery(this).get(0).options[i].value + ",";
        }
        return res.trims(",");
    }
}
/*获得当前选中项的值 */
jQuery.fn.getSelectedValue = function () {
    if (this.size() == 0) {
        //return "下拉框中无选中值"; 
        return "";
    }
    else {
        return jQuery(this).val();
    }
}
/*设置select中值为value的项为选中 */
jQuery.fn.setSelectedValue = function (value) {
    jQuery(this).get(0).value = value;
}
/*设置select中文本为text的第一项被选中 */
jQuery.fn.setSelectedText = function (text) {
    var isExist = false;
    var count = this.size();
    for (var i = 0; i < count; i++) {
        if (jQuery(this).get(0).options[i].text == text) {
            jQuery(this).get(0).options[i].selected = true;
            isExist = true;
            break;
        }
    }
    if (!isExist) {
        alert("下拉框中不存在该项");
    }
}
/*设置选中指定索引项 */
jQuery.fn.setSelectedIndex = function (index) {
    var count = this.size();
    if (index >= count || index < 0) {
        alert("选中项索引超出范围");
    }
    else {
        jQuery(this).get(0).selectedIndex = index;
    }
}
/*判断select项中是否存在值为value的项 */
jQuery.fn.isExistItem = function (value) {
    var isExist = false;
    var count = this.size();
    for (var i = 0; i < count; i++) {
        if (jQuery(this).get(0).options[i].value == value) {
            isExist = true;
            break;
        }
    }
    return isExist;
}
/*向select中添加一项，显示内容为text，值为value,如果该项值已存在，则提示 */
jQuery.fn.addOption = function (text, value) {
    if (this.isExistItem(value)) {
        alert(value);
    }
    else {
        jQuery(this).get(0).options.add(new Option(text, value));
    }
}
/*删除select中值为value的项，如果该项不存在，则提示 */
jQuery.fn.removeItem = function (value) {
    if (this.isExistItem(value)) {
        var count = this.size();
        for (var i = 0; i < count; i++) {
            if (jQuery(this).get(0).options[i].value == value) {
                jQuery(this).get(0).remove(i);
                break;
            }
        }
    }
    else {
        alert("待删除的项不存在!");
    }
}
/*删除select中指定索引的项 */
jQuery.fn.removeIndex = function (index) {
    var count = this.size();
    if (index >= count || index < 0) {
        alert("待删除项索引超出范围");
    }
    else {
        jQuery(this).get(0).remove(index);
    }
}
/*删除select中选定的项 */
jQuery.fn.removeSelected = function () {
    var index = this.getSelectedIndex();
    this.removeIndex(index);
}
/*清除select中的所有项 */
jQuery.fn.clearAll = function () {
    jQuery(this).get(0).options.length = 0;
}
/*===========================*/

//分页
//_index：当前页码
//_pageCount：总页数
//_recordCount：记录总数
//_pageSize：页大小
//_clickEvent：导航事件   
function Page(_index, _pageCount, _recordCount,_pageSize, _clickEvent) {
    _index += 1;
    var arrHtml = [];
    var prevPage = parseInt(_index) - 1;
    var nextPage = parseInt(_index) + 1;
    arrHtml.push('<span class="number">');
    if (prevPage < 1) {
        arrHtml.push('<span title="First">&#171;</span>');
        arrHtml.push('<span title="Prev">&#139;</span>');
    } else {
        arrHtml.push('<span title="First"><a href="javascript:' + _clickEvent + '(1);">&#171;</a></span>');
        arrHtml.push('<span title="Prev"><a href="javascript:' + _clickEvent + '(' + prevPage + ');">&#139;</a></span>');
    }
    if (_index % 10 == 0) {
        var startPage = _index - 9;
    } else {
        var startPage = _index - _index % 10 + 1;
    }
    if (startPage > 10)
        arrHtml.push('<span title="Previous 10 pages"><a href="javascript:' + _clickEvent + '(' + (startPage - 1) + ');">...</a></span>');
    for (var i = startPage; i < startPage + 10; i++) {
        if (i > _pageCount) break;
        if (i == _index) {
            arrHtml.push('<span title="Page ' + i + '">' + i + '</span>');
        } else {
            arrHtml.push('<span title="Page ' + i + '"><a href="javascript:' + _clickEvent + '(' + i + ');">' + i + '</a></span>');
        }
    }
    if (_pageCount >= startPage + 10)
        arrHtml.push('<span title="Next 10 pages"><a href="javascript:' + _clickEvent + '(' + (startPage + 10) + ');">...</a></span>');
    if (nextPage > _pageCount) {
        arrHtml.push('<span title="Next">&#155;</span>');
        arrHtml.push('<span title="Last">&#187;</span>');
    } else {
        arrHtml.push('<span title="Next"><a href="javascript:' + _clickEvent + '(' + nextPage + ');">&#155;</a></span>');
        arrHtml.push('<span title="Last"><a href="javascript:' + _clickEvent + '(' + _pageCount + ');">&#187;</a></span>');
    }
    arrHtml.push('<span>Page: ' + _index + '/' + _pageCount + ', Page Size:' + _pageSize + ', Total:' + _recordCount + '</span>');
    arrHtml.push('</span>');

    return arrHtml.join('');
}

//货币格式化数字
var zh = { symbol: '', positiveFormat: '%s%n', negativeFormat: '%s-%n', decimalSymbol: '.', digitGroupSymbol: ',', groupDigits: true };
var keyin = { symbol: '', colorize: true, negativeFormat: '-%s%n', roundToDecimalPlace: -1, eventOnDecimalsEntered: true }; //输入时
function outputMoney(number) {
    number += '';
    number = number.replace(/\,/g, "");
    if (number == "") return "";
    if (number < 0) {
        return '-' + outputDollars(Math.floor(Math.abs(number) - 0) + '') + outputCents(Math.abs(number) - 0);
    } else {
        return outputDollars(Math.floor(number - 0) + '') + outputCents(number - 0);
    }
}

function outputDollars(number) {
    if (number.length <= 3) {
        return (number == '' ? '0' : number);
    } else {
        var mod = number.length % 3;
        var output = (mod == 0 ? '' : (number.substring(0, mod)));
        for (i = 0; i < Math.floor(number.length / 3); i++) {
            if ((mod == 0) && (i == 0)) {
                output += number.substring(mod + 3 * i, mod + 3 * i + 3);
            } else {
                output += ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
            }
        }
        return (output);
    }
}

function outputCents(amount) {
    amount = Math.round(((amount) - Math.floor(amount)) * 100);
    return (amount < 10 ? '.0' + amount : '.' + amount);
}

//浮点数加法运算
function FloatAdd(arg1,arg2){
     var r1,r2,m;
     try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
     try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
     m=Math.pow(10,Math.max(r1,r2));
     return (arg1*m+arg2*m)/m;
}

//浮点数减法运算
function FloatSub(arg1,arg2){
     var r1,r2,m,n;
     try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
     try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
     m=Math.pow(10,Math.max(r1,r2));
     //动态控制精度长度
     n=(r1>=r2)?r1:r2;
     return ((arg1*m-arg2*m)/m).toFixed(n);
}

//浮点数乘法运算
function FloatMul(arg1,arg2)
{
     var m=0,s1=arg1.toString(),s2=arg2.toString();
     try{m+=s1.split(".")[1].length}catch(e){}
     try{m+=s2.split(".")[1].length}catch(e){}
     return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m);
}

  //浮点数除法运算
function FloatDiv(arg1,arg2){
     var t1=0,t2=0,r1,r2;
     try{t1=arg1.toString().split(".")[1].length}catch(e){}
     try{t2=arg2.toString().split(".")[1].length}catch(e){}
     with(Math){
         r1=Number(arg1.toString().replace(".",""));
         r2=Number(arg2.toString().replace(".",""));
         return (r1/r2)*pow(10,t2-t1);
     }
}

